from launch import LaunchDescription
from launch_ros.actions import Node, LifecycleNode
from launch.actions import TimerAction,ExecuteProcess
import launch_ros.actions

def generate_launch_description():
    # 使用 LifecycleNode
    receiver_node = LifecycleNode(
        name='socket_can_receiver_node',
        namespace='ugv',
        package='ros2_socketcan',
        executable='socket_can_receiver_node_exe',
        parameters=[{'interface': 'can0'}],
        output='screen',
    )
    
    sender_node = LifecycleNode(
        name='socket_can_sender_node',
        namespace='ugv',
        package='ros2_socketcan',
        executable='socket_can_sender_node_exe',
        parameters=[{'interface': 'can0'}],
        output='screen',
    )
    configure_receiver = ExecuteProcess(
        cmd=['ros2', 'lifecycle', 'set', '/ugv/socket_can_receiver_node', 'configure'],
        output='screen'
    )
    activate_receiver = ExecuteProcess(
        cmd=['ros2', 'lifecycle', 'set', '/ugv/socket_can_receiver_node', 'activate'],
        output='screen'
    )
    configure_sender = ExecuteProcess(
        cmd=['ros2', 'lifecycle', 'set', '/ugv/socket_can_sender_node', 'configure'],
        output='screen'
    )
    activate_sender = ExecuteProcess(
        cmd=['ros2', 'lifecycle', 'set', '/ugv/socket_can_sender_node', 'activate'],
        output='screen'
    )

    return LaunchDescription([
        receiver_node,
        sender_node,
        TimerAction(
            period=3.0,
            actions=[configure_receiver, configure_sender]
        ),
        TimerAction(
            period=5.0,
            actions=[activate_receiver, activate_sender]
        ),
        launch_ros.actions.Node(
            namespace='ugv',
            package='can_decode',
            executable='can_decode',
            output='screen'),
    ])